package service

import (
	"singo/model"
	"singo/serializer"
)

// ListProjectService 项目列表服务
type ListProjectService struct {
	Limit           int    `form:"pageSize"`
	Start           int    `form:"pageNo"`
	OrganizationUID string `form:"organization_uid"`
}

// ProjectList 项目列表
func (service *ListProjectService) ProjectList() serializer.Response {
	projects := []model.Project{}
	total := 0

	if service.Limit == 0 {
		service.Limit = 99
	}

	if err := model.DB.Model(&projects).Count(&total).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Order("created_at desc").Find(&projects).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	return serializer.BuildListResponse(serializer.BuildProjects(projects), uint(total))
}
